/**
 * @Description: test
 * @Author: Hu Outstanding
 * @Date: 2022/2/23 20:33
 */
package com.aaa.three.service.impl;

import com.aaa.three.mapper.AdminQueryRoleMapper;
import com.aaa.three.pojo.domain.*;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.AdminQueryRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminQueryRoleServiceImpl implements AdminQueryRoleService {
    @Autowired
    private AdminQueryRoleMapper adminQueryRoleMapper;

    private int a = 0;

    @Override
    public AjaxResult queryRole() {
        List<AdminRole> roles = adminQueryRoleMapper.queryRole();
        return AjaxResult.success(roles);

    }

    @Override
    public AjaxResult getRole(int id) {

        AdminRole getRole = adminQueryRoleMapper.getRole(id);
        return AjaxResult.success(getRole);
    }

    @Override
    public AjaxResult getPoption() {
        List<AdminPermission> getPoption = adminQueryRoleMapper.getPoption();

        return AjaxResult.success(getPoption);
    }

    @Override
    public AjaxResult querySOption(int id) {
        List<AdminPermission> querySOption = adminQueryRoleMapper.querySOption(id);

        return AjaxResult.success(querySOption);
    }

    /*@Override
    public AjaxResult getPermission() {
        List<AdminPermission> getPermission = adminQueryRoleMapper.getPermission();
        return AjaxResult.success(getPermission);

    }*/

    @Override
    public AjaxResult queryPermissionState(int id) {

        List<AdminPermission> queryPermissionState = adminQueryRoleMapper.queryPermissionState(id);

        return AjaxResult.success(queryPermissionState);
    }

    @Override
    public AjaxResult queryPpermission() {

        List<AdminPermission> queryPpermission = adminQueryRoleMapper.queryPpermission();

        return AjaxResult.success(queryPpermission);
    }

    @Override
    public AjaxResult listPermission(int id) {

        List<AdminPermission> listPermission = adminQueryRoleMapper.listPermission(id);
        return AjaxResult.success(listPermission);

    }

    @Override
    public AjaxResult updatePer(RolePer rolePer) {

        int rid = rolePer.getRid();
        String id = rolePer.getId();
        /*通过角色id清空该角色所拥有的权限*/
        int p = adminQueryRoleMapper.cleanPer(rid);

        /*将前台传过来的数据通过","转换成数据*/
        String[] split = id.split(",");
        /*便利数组*/
        for (int i = 0; i < split.length; i++) {
            /*将便利出来的数据转换成我们所需要的int类型*/
            int i1 = Integer.parseInt(split[i]);
            /*通过我们便利出来的子级权限ID来获取其父类权限的ID*/
            int z = adminQueryRoleMapper.queryPper(i1);
            /*通过父级ID获取是否有该角色的父级ID*/
            int countPper = adminQueryRoleMapper.queryCountPper(z,rid);

            if (countPper == 0){
                /*如果父级ID的数量为0,则添加其权限*/
                int addNewPper = adminQueryRoleMapper.addNewPper(z,rid);
                int perSper = adminQueryRoleMapper.perSper(i1,rid);
            }else
                {
                /*通过角色ID和子级权限ID来查询该子级权限是否存在*/
                int countSper = adminQueryRoleMapper.queryCountSper(i1,rid);
                if (countSper == 0){
                    /*如果该子级权限不存在，则添加子级权限*/
                    int addNewSper = adminQueryRoleMapper.addNewSper(i1,rid);
                }else {
                    /*修改父类权限的ID*/
                    int q = adminQueryRoleMapper.addPper(z,rid);
                    /*修改子级权限的ID*/
                    int qq = adminQueryRoleMapper.addSper(i1,rid);
                    if ( qq == 1 ){
                        a++;
                    }
                }
            }
        }
        if (a > 0){
            return AjaxResult.success();
        }
        return AjaxResult.fail("权限修改失败");
    }

    @Override
    public AjaxResult addRole(AddRole addRole) {
        String rname = addRole.getRname();
        int countName = adminQueryRoleMapper.countName(rname);
        if (countName == 0 ){
            int www = adminQueryRoleMapper.addRoleInfo(addRole);
            int newId =  addRole.getId();
            String pid = addRole.getRid();
            String[] split = pid.split(",");
            for (int i = 0; i < split.length; i++) {
                int i1 = Integer.parseInt(split[i]);
                int perid = adminQueryRoleMapper.query(i1);
                int o = adminQueryRoleMapper.countPerid(perid,newId);
                if ( o == 0){
                    int adad = adminQueryRoleMapper.addperid(perid,newId);
                }
                int qq = adminQueryRoleMapper.addNewRolePer(i1,newId);
                if ( qq == 1 ){
                    a++;
                }
            }
            if (a > 0){
                return AjaxResult.success();
            }
        }
        return AjaxResult.fail("角色已存在");
    }


    @Override
    public AjaxResult updateRoleInfo(AdminRolePermission adminRolePermission) {
        int i = adminQueryRoleMapper.updateRoleInfo(adminRolePermission);
        if (i > 0){
            return AjaxResult.success();
        }
        return AjaxResult.fail("信息修改失败");
    }



}
